package com.clayecloud.module_login.viewModels;

import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.clayecloud.lib_base.action.ToastAction;
import com.clayecloud.lib_common.apis.LoginApi;
import com.clayecloud.lib_common.apis.RegisterApi;
import com.clayecloud.lib_common.apis.entity.register.ClientRegister;
import com.clayecloud.lib_common.utils.HttpUtil;
import com.clayecloud.lib_common.utils.MessageUtil;
import com.clayecloud.lib_common.utils.TempData;
import com.clayecloud.module_login.entity.RegisterUser;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class RegisterActivityViewModel extends ViewModel implements ToastAction {

    private RegisterApi registerApi;

    /**
     * 注册页面数据
     */
    private MutableLiveData<RegisterUser> registerUserMutableLiveData;

    /**
     * 注册状态
     */
    private MutableLiveData<Boolean> registerState;

    public MutableLiveData<RegisterUser> getRegisterUserMutableLiveData() {
        return registerUserMutableLiveData;
    }

    public void setRegisterUserMutableLiveData(MutableLiveData<RegisterUser> registerUserMutableLiveData) {
        this.registerUserMutableLiveData = registerUserMutableLiveData;
    }

    public MutableLiveData<Boolean> getRegisterState() {
        return registerState;
    }

    public void setRegisterState(MutableLiveData<Boolean> registerState) {
        this.registerState = registerState;
    }

    //初始化数据
    public void leadData(){
        registerApi = HttpUtil.getInstance().create(RegisterApi.class); //初始化网络
        RegisterUser registerUser = new RegisterUser(false);
        MutableLiveData<RegisterUser> registerUserMutableLiveData = new MutableLiveData<>();
        registerUserMutableLiveData.setValue(registerUser);
        setRegisterUserMutableLiveData(registerUserMutableLiveData);
        MutableLiveData<Boolean> registerState = new MutableLiveData<>();
        registerState.setValue(false);
        setRegisterState(registerState);
    }

    //注册账号
    public boolean register(){
        RegisterUser registerUser = registerUserMutableLiveData.getValue();
        String str_username = registerUser.getStr_username();
        String str_email = registerUser.getStr_email();
        String str_phone = registerUser.getStr_phone();
        String str_loginPassword = registerUser.getStr_loginPassword();
        String str_verificationCode = registerUser.getStr_verificationCode();
        ClientRegister clientRegister = new ClientRegister(str_phone,str_verificationCode,str_loginPassword,str_username,str_email);
        registerApi.registerUser(clientRegister)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<MessageUtil>() {
                    private Disposable disposable;
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        disposable = d;
                    }
                    @Override
                    public void onNext(@NonNull MessageUtil messageUtil) {
                        if (messageUtil.getResult()){
                            toast("注册成功, 返回进行登录");
                            MutableLiveData<Boolean> registerState = getRegisterState();
                            registerState.setValue(true);
                        }
                        toast(messageUtil.getMsg());
                    }
                    @Override
                    public void onError(@NonNull Throwable e) {
                        toast("网络请求失败，请联系管理员");
                        Log.e("accountLogin",e.getMessage());
                        disposable.dispose();
                    }
                    @Override
                    public void onComplete() {
                        disposable.dispose();
                    }
                });
        return true;
    }


}
